﻿using ODS.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ODS.ViewModel
{
    public class DeviceViewModel : INotifyPropertyChanged
    {
        public DeviceViewModel(DeviceModel _deviceModel)
        {
            DeviceModel = _deviceModel;
            VehicleName = _deviceModel.VehicleName;
            RemainingRequestNumber = _deviceModel.RemainingRequestNumber;
            UnreadNumber = _deviceModel.UnreadNumber;
            ExpiresAt = _deviceModel.ExpiresAt;

        }

        DeviceModel deviceModel;

        public DeviceModel DeviceModel
        {
            get { return deviceModel; }
            set { 
                deviceModel = value;
            }
        }

        TotalMessageViewModel totalMessageViewModel;

        public TotalMessageViewModel TotalMessageViewModel
        {
            get { return totalMessageViewModel; }
            set { totalMessageViewModel = value; }
        }

        String vehicleName;

        public String VehicleName
        {
            get {
                vehicleName = vehicleName.ToUpper();
                return vehicleName;
            }
            set {
                vehicleName = value;
                NotifyPropertyChanged("VehicleName");
            }
        }

        String remainingRequestNumber;

        public String RemainingRequestNumber
        {
            get { return remainingRequestNumber; }
            set {
                remainingRequestNumber = int.Parse(value).ToString("D3");
                NotifyPropertyChanged("RemainingRequestNumber");
                NotifyPropertyChanged("ForeGroundRemainRequest");
            }
        }

        String unreadNumber;

        public String UnreadNumber
        {
            get {
                if (unreadNumber == null)
                    unreadNumber = "0";
                return unreadNumber;
            }
            set {
                unreadNumber = value;
                NotifyPropertyChanged("UnreadNumber");
            }
        }


        //String titleRequest;

        //public String TitleRequest
        //{
        //    get { return titleRequest; }
        //    set {
        //        titleRequest = value;
        //        NotifyPropertyChanged("TitleRequest");
        //        NotifyPropertyChanged("VisibilityDeviceInfo");
        //        NotifyPropertyChanged("VisibilityTitleRequest");
        //    }
        //}

        DateTime expiresAt;

        public DateTime ExpiresAt
        {
            get { return expiresAt; }
            set {
                expiresAt = value;
                NotifyPropertyChanged("VisibilityDeviceInfo");
                //NotifyPropertyChanged("VisibilityTitleRequest");
            }
        }

        String foreGroundRemainRequest;

        public String ForeGroundRemainRequest
        {
            get {
                if (int.Parse(RemainingRequestNumber) < 10)
                {
                    foreGroundRemainRequest = "Red";
                }
                else
                {
                    foreGroundRemainRequest = "White";
                }

                return foreGroundRemainRequest;
            }
            set {
                foreGroundRemainRequest = value;
                NotifyPropertyChanged("ForeGroundRemainRequest");
            }
        }

        String foreGroundUnreadMessage;

        public String ForeGroundUnreadMessage
        {
            get {
                if (int.Parse(unreadNumber) > 0)
                {
                    foreGroundUnreadMessage = "Red";
                }
                else
                {
                    foreGroundUnreadMessage = "White";
                }
                return foreGroundUnreadMessage; }
            set
            {
                foreGroundRemainRequest = value;
                NotifyPropertyChanged("ForeGroundUnreadMessage");
            }
        }

        Visibility visibilityDeviceInfo;

        public Visibility VisibilityDeviceInfo
        {
            get {

                if (this.deviceModel.IsActivatedSubscription )
                {
                    visibilityDeviceInfo = Visibility.Visible;
                    VisibilityRequestTitle = Visibility.Collapsed;
                }
                else
                {
                    visibilityDeviceInfo = Visibility.Collapsed;
                    VisibilityRequestTitle = Visibility.Visible;
                }

                return visibilityDeviceInfo;
            }
            set { 
                visibilityDeviceInfo = value;
                NotifyPropertyChanged("VisibilityDeviceInfo");
            }
        }

        Visibility visibilityRequestTitle;

        public Visibility VisibilityRequestTitle
        {
            get {
                return visibilityRequestTitle;
            }
            set {
                visibilityRequestTitle = value;
                NotifyPropertyChanged("VisibilityRequestTitle");
            }
        }





        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
